package com.example.LightningController;

import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.*;
import android.view.animation.Animation.AnimationListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class ZonesAdapter extends ArrayAdapter<Zone> {
	private ArrayList<Zone> items;
	private LayoutInflater vi;
	private MODES mode;
	private Context context;
	private ZonesViewHolder zonesViewHolder;
	private ArrayList<ZonesViewHolder> zonesViewHolderList = new ArrayList<ZonesViewHolder>();
	private int trigger = -1;
//	private ArrayList<Zone> zonesList;

	public enum MODES {
		EDIT_MODE(1), NON_EDIT_MODE(0);

		private int mode_identifier;

		private MODES(int mode_identifier) {
			this.mode_identifier = mode_identifier;
		}
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public boolean isEnabled(int position) {
//		if (items.get(position) instanceof ZoneDetails) {
//			return false;
//		} else {
			return true;
//		}
	}

	public ZonesAdapter(Context context, ArrayList<Zone> items, MODES mode,
			int trigger) {
		super(context, 0, items);
		this.context = context;
		this.mode = mode;
		this.items = items;
		this.trigger = trigger;
//		this.zonesList = Session.getInstance().getCurrentUnit().getZBService().getZoneManager().getZones();

		vi = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		zonesViewHolder = new ZonesViewHolder();

		View v = convertView;

		final Item i = items.get(position);
		if (i != null) {
			if (i.isSection()) {
				ZoneDetails si = (ZoneDetails) i;
				v = vi.inflate(R.layout.zones_list_item_detailed, null);

				v.setOnClickListener(null);
				v.setOnLongClickListener(null);
				v.setLongClickable(false);

				zonesViewHolder.zoneTemperatureLayout = (RelativeLayout) v
						.findViewById(R.id.zones_list_item_detailed_temperature_layout);

				zonesViewHolder.zonesTemperatureView = (TextView) v
						.findViewById(R.id.zones_list_item_detailed_temperature_text);

				zonesViewHolder.zonesDetailsTextView = (TextView) v
						.findViewById(R.id.zones_list_item_detailed_text);

				String[] zoneDetails = si.getZoneDetails();

				String tempConstructedString = "";
				for (String zoneDetail : zoneDetails) {
					tempConstructedString += "&#8226; " + zoneDetail + "<br/>";
				}

				if (zoneDetails.length < 2)
					tempConstructedString += "<br/>";

				if (zonesViewHolder.zonesDetailsTextView != null)
					zonesViewHolder.zonesDetailsTextView.setText(Html
							.fromHtml(tempConstructedString));
//				if (zonesViewHolder.zonesTemperatureView != null)
//					if (!this.zonesList.get(i.getID()).getTemperatsure()
//							.equals("-71")) {
//						zonesViewHolder.zoneTemperatureLayout
//								.setVisibility(View.VISIBLE);
//						zonesViewHolder.zonesTemperatureView
//								.setText(this.zonesList.get(i.getID())
//										.getTemperature() + "\u00B0");
//					}

			} else {

				final Zone ei = (Zone) i;
				v = vi.inflate(R.layout.zones_list_item_selection, null);

				zonesViewHolder.zoneSelectionLayout = (RelativeLayout) v
						.findViewById(R.id.zones_list_item_selection_editable_movable_layout);

				zonesViewHolder.zoneSelectionText = (TextView) v
						.findViewById(R.id.zones_list_item_selection_text);

				zonesViewHolder.zoneSelectionImage = (ImageView) v
						.findViewById(R.id.zones_list_item_selection_editable_icon);

				zonesViewHolder.zoneSelectionArrowImage = (ImageView) v
						.findViewById(R.id.zones_list_item_selection_arrow);

                if (ei.getState() == Zone.ButtonState.OFF){
                    zonesViewHolder.zoneSelectionArrowImage.setImageResource(R.drawable.button_on);
                }else{
                    zonesViewHolder.zoneSelectionArrowImage.setImageResource(R.drawable.button_off);
                }

                zonesViewHolder.zoneSelectionArrowImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                               if (ei.getState() == Zone.ButtonState.OFF){
                                   ei.setState(Zone.ButtonState.ON);
                                   zonesViewHolder.zoneSelectionArrowImage.setImageResource(R.drawable.button_on);
                                   notifyDataSetChanged();
                                   notifyDataSetInvalidated();
                               }else{
                                   ei.setState(Zone.ButtonState.OFF);
                                   zonesViewHolder.zoneSelectionArrowImage.setImageResource(R.drawable.button_off);
                                   notifyDataSetChanged();
                                   notifyDataSetInvalidated();
                               }
                    }
                });

				zonesViewHolder.zoneSelectionText.setText(ei.getTag());

				final AnimationSet set = new AnimationSet(true);

				// adjusting settings
				if (this.mode == MODES.EDIT_MODE) {
					Animation editAnimation = getEditModeAnimation();
					Animation alphaAnimation = getEditModeAlphaAnimation();
					zonesViewHolder.zoneSelectionImage
							.setAnimation(alphaAnimation);
					set.addAnimation(alphaAnimation);

					if (this.trigger == 1) {
						zonesViewHolder.zoneSelectionLayout
								.setAnimation(editAnimation);
						zonesViewHolder.zoneSelectionArrowImage
								.setAnimation(editAnimation);

						editAnimation
								.setAnimationListener(new AnimationListener() {

                                    @Override
                                    public void onAnimationStart(
                                            Animation animation) {
                                        if (zonesViewHolder.zoneSelectionLayout != null) {
                                            LayoutAnimationController controller = new LayoutAnimationController(
                                                    set, 0.25f);
//                                            zonesViewHolder.zoneSelectionLayout
//                                                    .setLayoutAnimation(controller);
                                        }
                                    }

                                    @Override
                                    public void onAnimationRepeat(
                                            Animation animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(Animation arg0) {
                                        ZonesAdapter.this.trigger = -2;
                                    }
                                });
					} else if (this.trigger == -2) {
						if (zonesViewHolder.zoneSelectionLayout != null)
							zonesViewHolder.zoneSelectionImage.clearAnimation();
						((RelativeLayout.LayoutParams) zonesViewHolder.zoneSelectionLayout
								.getLayoutParams()).setMargins(0, 0, 0, 0);
					}
				} else {
					Animation nonEditAnimation = getNonEditModeAnimation();
					Animation alphaAnimation = getNonEditModeAlphaAnimation();
					zonesViewHolder.zoneSelectionImage
							.setAnimation(alphaAnimation);
					set.addAnimation(alphaAnimation);

					if (this.trigger == 0) {
						zonesViewHolder.zoneSelectionLayout
								.setAnimation(nonEditAnimation);
						zonesViewHolder.zoneSelectionArrowImage
								.setAnimation(nonEditAnimation);

						nonEditAnimation
								.setAnimationListener(new AnimationListener() {

                                    @Override
                                    public void onAnimationStart(
                                            Animation animation) {
                                        if (zonesViewHolder.zoneSelectionLayout != null) {
                                            LayoutAnimationController controller = new LayoutAnimationController(
                                                    set, 0.25f);
//                                            zonesViewHolder.zoneSelectionLayout
//                                                    .setLayoutAnimation(controller);
                                        }
                                    }

                                    @Override
                                    public void onAnimationRepeat(
                                            Animation animation) {
                                    }

                                    @Override
                                    public void onAnimationEnd(
                                            Animation animation) {
                                        ZonesAdapter.this.trigger = -2;
                                    }
                                });
					} else if (this.trigger == -2) {
						if (zonesViewHolder.zoneSelectionLayout != null)
							zonesViewHolder.zoneSelectionImage.clearAnimation();
						((RelativeLayout.LayoutParams) zonesViewHolder.zoneSelectionLayout
								.getLayoutParams()).setMargins(-(int)
								convertDpToPixel(51, this.context), 0, 0, 0);
					}
				}
			}
		}

		zonesViewHolder.setTag(zonesViewHolder.toString());
		zonesViewHolder.setFlag(1);
		zonesViewHolderList.add(zonesViewHolder);
		return v;
	}

    public static float convertDpToPixel(float dp, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return px;
    }

	public MODES getMode() {
		return this.mode;
	}

	public ArrayList<ZonesViewHolder> getZonesViewHolderList() {
		return this.zonesViewHolderList;
	}

	public Animation getEditModeAnimation() {
		TranslateAnimation animation = new TranslateAnimation(0,
				convertDpToPixel(57, this.context), 0, 0);
		animation.setDuration(300);
		animation.setFillAfter(true);

		return animation;
	}

	public Animation getNonEditModeAnimation() {
		TranslateAnimation animation = new TranslateAnimation(
				convertDpToPixel(57, this.context), 0, 0, 0);
		animation.setDuration(300);
		animation.setFillAfter(true);

		return animation;
	}

	public Animation getEditModeAlphaAnimation() {
		AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(250);
		animation.setStartOffset(50);
		return animation;
	}

	public Animation getNonEditModeAlphaAnimation() {
		AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
		animation.setDuration(250);
		animation.setStartOffset(50);
		return animation;
	}
}
